package com.winapi.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.winapi.project.common.ErrorCode;
import com.winapi.project.exception.BusinessException;
import com.winapi.project.mapper.InterfaceInfoMapper;
import com.winapi.project.model.vo.InterfaceInfoVO;
import com.winapi.project.service.InterfaceInfoService;
import com.winapi.project.service.UserInterfaceInfoService;
import com.winapi.winapicommon.model.entity.InterfaceInfo;
import com.winapi.winapicommon.model.entity.UserInterfaceInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 张莹莹
* @description 针对表【interface_info】的数据库操作Service实现
* @createDate 2023-11-29 16:14:21
*/
@Service
public class InterfaceInfoServiceImpl extends ServiceImpl<InterfaceInfoMapper, InterfaceInfo>
    implements InterfaceInfoService {
@Resource
private UserInterfaceInfoService userInterfaceInfoService;
    @Override
    public void validInterfaceInfo(InterfaceInfo interfaceInfo, boolean add) {
        if (interfaceInfo == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String name = interfaceInfo.getName();
        String url = interfaceInfo.getUrl();
        String method = interfaceInfo.getMethod();
        String userRequestParams = interfaceInfo.getUserRequestParams();
        // 创建时，所有参数必须非空
        if (add) {
            if (StringUtils.isAnyBlank(name,url,method,userRequestParams)) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
        }
        if (StringUtils.isNotBlank(name) && name.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "名称过长");
        }
        if(!method.equals("POST")&&!method.equals("GET")&&!method.equals("PUT")&&!method.equals("DELETE")){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求方法不存在");
        }
    }

    @Override
    public List<InterfaceInfoVO> topNumber() {
        List<UserInterfaceInfo> userInterfaceInfos = userInterfaceInfoService.topNumber();
        Map<Long,List<UserInterfaceInfo>> interfaceInfoIdobjMap= userInterfaceInfos.stream()
                .collect(Collectors.groupingBy(UserInterfaceInfo::getInterfaceInfoId));
        QueryWrapper<InterfaceInfo> queryWrapper =new QueryWrapper<>();
        queryWrapper.in("id",interfaceInfoIdobjMap.keySet());
        List<InterfaceInfo>list =this.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        List<InterfaceInfoVO> interfaceInfoVOList= list.stream().map(interfaceInfo -> {
            InterfaceInfoVO interfaceInfoVO = new InterfaceInfoVO();
            BeanUtils.copyProperties(interfaceInfo, interfaceInfoVO);
            int totalNum = interfaceInfoIdobjMap.get(interfaceInfo.getId()).get(0).getTotalNum();
            interfaceInfoVO.setTotalNum(totalNum);
            return interfaceInfoVO;
        }).collect(Collectors.toList());
        return interfaceInfoVOList;
    }

}




